#coding:utf-8
from django.template import RequestContext
from django.shortcuts import render_to_response
from django_user_agents.utils import get_user_agent
from lovewith.views import site_info
from lovewith.album.views.modules.album import AlbumApi
from lovewith.share.views.modules.attach import AttachApi
from lovewith.promote.views.modules.home import HomeApi
from lovewith.website.views.modules.category import CategoryApi
from lovewith.share.views.modules.attach_flag import AttachFlagApi
from lovewith.website.views.modules.color import ColorApi


#显示首页数据
def get_home_data(request):
    #判断UA
    user_agent = get_user_agent(request)

    if user_agent.is_mobile:
        #精选
        choice_data = HomeApi.get_home_choice_data(is_mobile=True)

        return render_to_response('mobile/home.jinja', {
            'cates': CategoryApi.get_all(),
            'colors': ColorApi.get_all(),
            'share_data': choice_data,
        }, context_instance=RequestContext(request, processors=[site_info]))
    else:
        #轮播数据
        home_cycle_data = HomeApi.get_home_cycle_data()
        #精选
        choice_data = HomeApi.get_home_choice_data(is_mobile=False)
        #栏目
        column_data = HomeApi.get_home_column_data()
        #推荐灵感板
        home_album = HomeApi.get_home_album()
        #数量
        goods_count = AttachFlagApi.get_goods_count()
        album_count = AlbumApi.get_total()
        image_count = AttachApi.get_total()

        response = render_to_response('share/home.jinja', {
            'promote_data': home_cycle_data,
            'share_data': choice_data,
            'column_data': column_data,
            'album': home_album,
            'album_count': album_count,
            'image_count': image_count
        }, context_instance=RequestContext(request, processors=[site_info]))

        return response